package com.horizon.ContentProvider;
import com.horizon.ContentProvider.MyContentProvider;


import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class SearchActivity extends Activity implements android.view.View.OnClickListener{
	EditText etCondition;
	ListView lv;
	ContentResolver cr;
	Button btInsert, btSearch;
	MyContentObserver mco;
	private static int count=0;
	
	//public static final Uri myDataUri = Uri.parse("content://com.horizon.ContentProvider.MyContentProvider.mohsen");
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        etCondition = (EditText) findViewById(R.id.editText1);
        lv = (ListView) findViewById(R.id.lvContactDetails);
        btInsert = (Button)findViewById(R.id.btInsert);
        btSearch = (Button)findViewById(R.id.btSearch);
        
        btInsert.setOnClickListener(this);
        btSearch.setOnClickListener(this);
 
        cr = getContentResolver();
        mco = new MyContentObserver(new android.os.Handler());
        // register my content observer
		Log.d("Mohsen", "SearchActivity-onCreate-> registering contentObserver");
        cr.registerContentObserver(MyContentProvider.CONTENT_URI, true, mco);
    }
    
    private Uri insertForSearch(String name, String pass, String phone, String email, String url){
    
    	if(name == null | pass == null){
    		Toast.makeText(getApplicationContext(), "Name and Password are required!", Toast.LENGTH_SHORT).show();
    		return null;
    	}
    		
    	Uri uri;
    	// Defines an object to contain the new values to insert
        ContentValues mNewValues = new ContentValues();

        /*
         * Sets the values of each column and inserts the word. The arguments to the "put"
         * method are "column name" and "value"
         */
        mNewValues.put(MyContentProvider.NAME, name);
        mNewValues.put(MyContentProvider.PWD, pass);
        mNewValues.put(MyContentProvider.PHONE, phone);

        mNewValues.put(MyContentProvider.EMAIL, email);
        mNewValues.put(MyContentProvider.COL_URL, url);
        
        Log.d("Mohsen", "SearchActivity-insert: inserting values to MyContentProvider");
        uri = cr.insert(MyContentProvider.CONTENT_URI,mNewValues);
    	return uri;
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btSearch:
			Log.d("Mohsen", "SearchActivity-onClick:");
			String[] mproj = {
				MyContentProvider.CONTENT_ID,
				MyContentProvider.NAME,
				MyContentProvider.PWD,
				MyContentProvider.PHONE,
				MyContentProvider.EMAIL,
				MyContentProvider.COL_URL
			};
			
		    Cursor tutorialCursor = cr.query(MyContentProvider.CONTENT_URI,
		            		mproj, null, null, null);
		    
		    if (tutorialCursor.moveToFirst()) {
		        String tutorialUrl = tutorialCursor.getString(0);
		        //tutSelectedListener.onTutSelected(tutorialUrl);
				Log.d("Mohsen", "SearchActivity-onClick:"+ tutorialUrl);

		    }
		    
		    tutorialCursor.moveToFirst();
				while(!tutorialCursor.isAfterLast()){
					Log.d("Mohsen", "SearchActivity-onClick:" + "id " + tutorialCursor.getLong(0));
					Log.d("Mohsen", "SearchActivity-onClick:" + "name " + tutorialCursor.getString(1));
					Log.d("Mohsen", "SearchActivity-onClick:" + "pass " + tutorialCursor.getString(2));
					Log.d("Mohsen", "SearchActivity-onClick:" + "phone " + tutorialCursor.getString(3));
					Log.d("Mohsen", "SearchActivity-onClick:" + "email " + tutorialCursor.getString(4));
					Log.d("Mohsen", "SearchActivity-onClick:" + "url " + tutorialCursor.getString(5));
					Log.d("Mohsen", "SearchActivity-onClick: +*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*");
					tutorialCursor.moveToNext();
				}
			
		    tutorialCursor.close();
			Toast.makeText(getBaseContext(), "Search Finished!",Toast.LENGTH_SHORT).show();
			break;
		case R.id.btInsert:
			 // Give some data to content provider
	        // Defines a new Uri object that receives the result of the insertion
	        Uri mNewUri; 
			int i = count;
	        	mNewUri = insertForSearch("user" + i, "pass" + i, "0333898754", "user" + i + "@def.com","url@def.com" );
	        	if(mNewUri == null){
	        		Log.d("Mohsen", "user" + i + "insertion failed");
	       	}else{
	       		count++;
	       		Log.d("Mohsen", "user" + i + "inserted");
			}
	        break;
		default:
			break;
		}
			
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		Log.d("Mohsen", "SearchActivity-onBackPressed");
		setContentView(R.layout.main);
	}
    
    
	
    
}